import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../../domain/entities/note.dart';
import '../../../domain/entities/note_request.dart'; // 添加请求实体导入
import '../../../core/offline/offline_mode_manager.dart'
    hide offlineModeManagerProvider, OfflineModeManager; // 添加离线模式管理器
import 'dependencies.dart'; // 添加依赖导入

/// 笔记状态
abstract class NoteState {}

class NoteInitial extends NoteState {}

class NoteLoading extends NoteState {}

class NotesLoaded extends NoteState {
  final List<Note> notes;
  NotesLoaded(this.notes);
}

class NoteLoaded extends NoteState {
  final Note note;
  NoteLoaded(this.note);
}

class NoteError extends NoteState {
  final String message;
  NoteError(this.message);
}

/// 笔记状态通知器
class NoteStateNotifier extends Notifier<NoteState> {
  @override
  NoteState build() {
    return NoteInitial();
  }

  /// 获取笔记列表
  Future<void> loadNotes({
    int? projectId,
    int? todoId,
    String? keyword,
    List<String>? tags,
  }) async {
    state = NoteLoading();

    try {
      final noteRepository = ref.read(noteRepositoryProvider);
      final notes = await noteRepository.getNotes(
        projectId: projectId,
        todoId: todoId,
        keyword: keyword,
        tags: tags,
      );

      state = NotesLoaded(notes);
    } catch (e) {
      state = NoteError(e.toString());
    }
  }

  /// 获取笔记详情
  Future<void> loadNote(int noteId) async {
    state = NoteLoading();

    try {
      final noteRepository = ref.read(noteRepositoryProvider);
      final note = await noteRepository.getNote(noteId);

      state = NoteLoaded(note);
    } catch (e) {
      state = NoteError(e.toString());
    }
  }

  /// 创建笔记
  Future<void> createNote(NoteCreateRequest request) async {
    try {
      final noteRepository = ref.read(noteRepositoryProvider);
      await noteRepository.createNote(request);

      // 重新加载笔记列表
      await loadNotes();
    } catch (e) {
      state = NoteError('创建笔记失败：${e.toString()}');
    }
  }

  /// 更新笔记
  Future<void> updateNote(int noteId, NoteUpdateRequest request) async {
    try {
      final noteRepository = ref.read(noteRepositoryProvider);
      await noteRepository.updateNote(noteId, request);

      // 重新加载笔记
      await loadNote(noteId);
    } catch (e) {
      state = NoteError('更新笔记失败：${e.toString()}');
    }
  }

  /// 删除笔记
  Future<void> deleteNote(int noteId) async {
    try {
      final noteRepository = ref.read(noteRepositoryProvider);
      await noteRepository.deleteNote(noteId);

      // 重新加载笔记列表
      await loadNotes();
    } catch (e) {
      state = NoteError('删除笔记失败：${e.toString()}');
    }
  }

  /// 检查当前是否处于离线模式
  bool isOfflineMode() {
    final offlineManager = ref.read(offlineModeManagerProvider);
    return offlineManager.currentStatus != OfflineModeStatus.online;
  }
}

/// 笔记状态Provider
final noteStateProvider =
    NotifierProvider<NoteStateNotifier, NoteState>(NoteStateNotifier.new);

/// 笔记列表Provider
final notesProvider = Provider<List<Note>>((ref) {
  final noteState = ref.watch(noteStateProvider);
  if (noteState is NotesLoaded) {
    return noteState.notes;
  }
  return [];
});

/// 当前笔记Provider
final currentNoteProvider = Provider<Note?>((ref) {
  final noteState = ref.watch(noteStateProvider);
  if (noteState is NoteLoaded) {
    return noteState.note;
  }
  return null;
});

/// 离线模式状态Provider
final offlineModeStatusProvider = Provider<OfflineModeStatus>((ref) {
  final offlineManager = ref.read(offlineModeManagerProvider);
  return offlineManager.currentStatus;
});
